package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :chenjie
 * @date :Created 2023/2/4 22:26
 */
@Transactional
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Override
    public List<TeachplanDto> getTreeNodes(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto dto) {
        Long id = dto.getId();
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(id ==null){//新增
            teachplan=new Teachplan();
            BeanUtils.copyProperties(dto,teachplan);
            teachplan.setCreateDate(LocalDateTime.now());
            int teachplanCount = getTeachplanCount(dto.getCourseId(), dto.getParentid());
            teachplan.setOrderby(teachplanCount+1);
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            BeanUtils.copyProperties(dto,teachplan);
            teachplan.setChangeDate(LocalDateTime.now());
            teachplanMapper.updateById(teachplan);

        }

    }

    @Override
    public void removeTeachplanByid(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        int count = getTeachplanCount(teachplan.getCourseId(), teachplan.getParentid());
        if(teachplan.getGrade().equals(1)){

                LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getParentid,id);
                teachplanMapper.delete(queryWrapper);
                teachplanMapper.deleteById(id);

        }else {
            teachplanMapper.deleteById(id);
            for (int i = teachplan.getOrderby()+1; i <= count; i++) {
                LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getOrderby,(long)i);
                queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
                Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
                teachplan1.setOrderby(i-1);
                teachplanMapper.updateById(teachplan1);
            }
        }


    }

    @Override
    public void moveupByid(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(teachplan.getOrderby().equals(1)){
            XueChengPlusException.cast("已是最顶部");
        }

        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getOrderby,(long)teachplan.getOrderby()-1);
        queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        queryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
        teachplan.setChangeDate(LocalDateTime.now());
        teachplan.setOrderby(teachplan1.getOrderby());
        teachplan1.setChangeDate(LocalDateTime.now());
        teachplan1.setOrderby(teachplan1.getOrderby()+1);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplan1);


    }

    @Override
    public void movedownByid(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(teachplan.getOrderby().equals(getTeachplanCount(teachplan.getCourseId(),teachplan.getParentid()))){
            XueChengPlusException.cast("已是最底部");
        }

        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getOrderby,(long)teachplan.getOrderby()+1);
        queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        queryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
        teachplan.setChangeDate(LocalDateTime.now());
        teachplan.setOrderby(teachplan1.getOrderby());
        teachplan1.setChangeDate(LocalDateTime.now());
        teachplan1.setOrderby(teachplan1.getOrderby()-1);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplan1);
    }

    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        return teachplanMapper.selectCount(queryWrapper);

    }
}
